package org.boot.easyapi.core.controller.table;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.boot.easyapi.core.cache.CacheRedisDAO;
import org.boot.easyapi.core.service.table.TableService;
import org.boot.easyapi.pojo.db.DbDTO;
import org.boot.easyapi.pojo.db.TableDTO;
import org.boot.easyapi.pojo.web.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import static org.boot.easyapi.pojo.web.CommonResult.success;


@RestController
@RequestMapping("/easyapi/table")
@Tag(name = "EasyApi - 表结构管理")
@Validated
@Slf4j
public class TableController {

    @Resource(name = "easyapi-tableService")
    private TableService tableService;

    @Resource(name = "easyapi-cacheRedisDAO")
    private CacheRedisDAO cacheRedisDAO;

    @PostMapping("/createTable")
    @Operation(summary = "创建表")
    public CommonResult<Boolean> createTable(@RequestBody Map<String, Object> tableInfo) {
        tableService.createTable(tableInfo);
        return success(true);
    }

    @GetMapping("/getDb")
    @Operation(summary = "获取当前数据库信息")
    public CommonResult<DbDTO> getDbType() throws SQLException {
        return success(tableService.getDbType());
    }

//    @GetMapping("/mysql/initTables")
//    @Operation(summary = "重载/载入所有表结构信息(mysql)")
//    public CommonResult<Boolean> initTables() {
//        cacheRedisDAO.setMysqlTables();
//        return success(true);
//    }

    @GetMapping("/initTableOne")
    @Operation(summary = "重载/载入指定表结构信息")
    public CommonResult<Boolean> initTableOne(@RequestParam("tableName") String tableName) {
        cacheRedisDAO.setTableByName(tableName);
        return success(true);
    }

    @GetMapping("/deleteCache")
    @Operation(summary = "删除所有表信息缓存")
    public CommonResult<Boolean> deleteCache() {
        cacheRedisDAO.delete();
        return success(true);
    }



    @PostMapping("/getNameList")
    @Operation(summary = "获取表名列表（缓存）")
    public CommonResult<List<String>> getList() {
        List<String> list = tableService.getTableList();
        return success(list);
    }

    @GetMapping("/get")
    @Operation(summary = "获取表结构信息（缓存）")
    public CommonResult<List<TableDTO>> getTable(@RequestParam("tableName") String tableName)  {
        return success(tableService.getTable(tableName));
    }

    @GetMapping("/getFields")
    @Operation(summary = "获取表字段信息（缓存）")
    public CommonResult<List<String>> getTableFields(@RequestParam("tableName") String tableName)  {
        return success(tableService.getTableFields(tableName));
    }


    @GetMapping("/upSystemTable")
    @Operation(summary = "升级数据表为系统表")
    public CommonResult<Boolean> upSystemTable(@RequestParam("table") String table)  {
        return success(tableService.upSystemTable(table), "系统字段插入成功，请继续查改主键字段名是否为：id");
    }


}